
from SAGIN_Progrem.entity.Structure import Queue
from SAGIN_Progrem.entity.Formulas import Formulas
import numpy as np
import random
from threading import Lock, Thread


class Cloud():
    B = 1000000  # 带宽1MHz
    P = 30  # 传输功率 30dBm
    f = 9000000000  # CPU频率 10GC/s
    formulas = Formulas(B, P, f)
    def __init__(self):
        self.computing_queue = Queue(20)


    # 云服务器在本地计算任务
    def compute_task(self, task_list, step_time, step):
        round_ = step_time
        delay_w_temp = 0  # 用于记录等待时间
        num = 0
        try:
            task_list_len = len(task_list)
        except:
            task_list_len = -1
        while True:
            while not self.computing_queue.isMAX():
                if num < task_list_len:
                    self.computing_queue.enQueue(task_list[num])
                    num += 1
                else:
                    break
            for i in range(self.computing_queue.size()):
                task = self.computing_queue.get_hand()
                task_index = task.get_task_index()
                delay = self.formulas.compute_Delay(task)
                if round_ >= delay:
                    # app_list[task.id].task_app_one_list[task_index].delay['wait'] += delay_w_temp
                    task.delay['compute'] += round(delay, 2)
                    task.finish = 3
                    task.finish_step = step
                    task.size_ = task.size
                    delay_w_temp += round(delay, 2)
                    round_ -= delay
                    self.computing_queue.deQueue()
                else:
                    task.delay['compute'] += round_
                    delay_w_temp += round_
                    size_down = ((round_ / 1000) * self.f) / task.complex
                    task.size_ -= size_down
                    for j in range(0, self.computing_queue.size()):
                        self.computing_queue.items[j].delay['wait'] += delay_w_temp
                    while not self.computing_queue.isMAX():
                        if num < task_list_len:
                            self.computing_queue.enQueue(task_list[num])
                            num += 1
                        else:
                            return
                    return
            if num >= task_list_len:
                return
    def stop(self):
        self.termination = True

if __name__ == '__main__':

    # 生成柏松分布的包裹数量
    lam = 5 # 柏松分布的参数
    num_packages = np.random.poisson(lam)

    # 生成每个包裹的到达时间间隔
    mean_interval = 10 # 负指数分布的参数
    arrivals = []
    for i in range(num_packages):
        interval = random.expovariate(1/mean_interval)
        if i == 0:
            arrivals.append(interval)
        else:
            arrivals.append(arrivals[-1] + interval)

    print('包裹到达时间间隔：', arrivals)




    pass








